package com.lanyuan.a.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.bean.Window;
import com.lanyuan.a.bean.WindowSale;
import com.lanyuan.a.dao.BaseDao;
import com.lanyuan.a.service.WindowSaleService;
import com.lanyuan.plugin.PageView;
@Service("windowSaleService")
public class WindowSaleServiceImpl implements WindowSaleService {
	@Autowired
	public BaseDao<WindowSale> baseDao;
	
	@Override
	public List<WindowSale> list() {
		return baseDao.find("from WindowSale");
	}

	@Override
	public List<WindowSale> queryByPage(BaseFormMap formMap) {
		Long count = null;
		String ss = (String)formMap.get("accountname");
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		if(ss != null && !ss.equals("")){
			String hql = "from User u where accountname like :accountname";
			String countHql = "select count (1) from User u where accountname like ?";
			count = baseDao.getCountByConditionLike(countHql , new Object[]{ss});
			pageView.setRowCount(count);
			return baseDao.find(hql, new Object[]{"%"+ss+"%"}, first, max);
		}else{
			String countHql = "select count (1) from WindowSale u ";
			count = baseDao.count(countHql);
			pageView.setRowCount(count);
			return baseDao.find("from WindowSale u ", new Object[]{}, first , max);
		}
	}

	@Override
	public List<WindowSale> list(String wid) {
		return baseDao.find("from WindowSale where w_id = ?",new Object[]{Integer.parseInt(wid)});
	}

	@Override
	public List<WindowSale> queryWidByPage(BaseFormMap formMap, String wid , String orderType , String orderDirection) {
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		
		String countHql = "select count (1) from WindowSale u where w_id = ?";
		Long count = baseDao.count(countHql, new Object[]{Integer.parseInt(wid)});
		pageView.setRowCount(count);
		return baseDao.find("from WindowSale u where u.w_id = ?  order by "+orderType+"  desc ", new Object[]{Integer.parseInt(wid)}, first , max);
	}
	
	@Override
	public List<WindowSale> queryFtypeByPage(BaseFormMap formMap, String ftype , String orderType , String orderDirection) {
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		
		String countHql = "select count (1) from WindowSale u where f_type = ?";
		Long count = baseDao.count(countHql, new Object[]{ftype});
		pageView.setRowCount(count);
		return baseDao.find("from WindowSale u where u.f_type = ?  order by "+orderType+"  desc ", new Object[]{ftype}, first , max);
	}

	@Override
	public WindowSale getByWFid(int wfid) {
		return baseDao.get("from WindowSale u where u.w_f_id = ?", new Object[]{wfid});
	}

	@Override
	public void addSale(WindowSale ws) {
		baseDao.save(ws);
	}

	@Override
	public List<WindowSale> queryByWid(int wid, Date timebegin , Date timeend) {
		String hql = "from WindowSale ws where ws.wid = ? and saledate > timebegin and saledate < timeend";
		return baseDao.find(hql, new Object[]{wid , timebegin , timeend});
	}

}
